package com.espirit.eap.manager.workflow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.espirit.eap.manager.I18n;
import com.espirit.eap.manager.I18nService;
import com.espirit.eap.pagelayout.FunctionButton;
import com.espirit.eap.pagelayout.ModelService;
import com.espirit.eap.sa.PrivilegeService;
import com.espirit.eap.sa.ScopeType;
import com.espirit.eap.util.StatGenericJPAService;
import com.googlecode.cswish.annotation.GenericType;
import com.googlecode.cswish.struts.spring.CacheService;

@Service
public class WFTemplateService {
	
	@Resource
	private StatGenericJPAService genericService;
	
	@Resource
	private ModelService modelService;
	
	@Resource
	private PrivilegeService privilegeService;
	
	@Resource
	private CacheService cacheService;
	
	@Resource
	I18nService i18nService;
	private List<Map<String,String>> srcEventItems;
	
	@PostConstruct
	private void init() {
		srcEventItems = cacheService.newList(null);
	}
	
	public void add(@GenericType Object model){
		WFTemplate template = (WFTemplate)model;
		prepareTemplate(template);
		genericService.add(template);
	}
	
	public Object _update(@GenericType Object model) {
    	WFTemplate template = (WFTemplate)model;
    	template = genericService.load(template);
    	WFCondition condition = template.safeWFCondtion();
    	if (condition.getAcceptTrigger() == null){
    		condition.setAcceptTrigger(WFCondition.ACCEPT_TRIGGER_ALWAYS);
    	}
    	
    	if (template.getEvents() != null){
    		String [] events = template.getEvents().split(",");
    		template.setSelectedEvents(events);
    	}
    	List<WFPhase> phases = template.getPhases();
    	if (phases != null ) {
			for(int i = 0; i < phases.size() ; i++){
				WFPhase phase = phases.get(i);
				phase.setWfPhases(phase);
			}
		}
    	template.setWfPhases(phases);
		return genericService.view(model);
    }
	
	public void update(@GenericType Object model){
		WFTemplate template = (WFTemplate)model;
		prepareTemplate(template);
		
		genericService.update(model);
	}
	
	private void prepareTemplate(WFTemplate model){
		WFTemplate template = (WFTemplate)model;
		template.initWFPhases();
		
		if (template.getSelectedEvents() != null){
			String events = "";
			for(String event : template.getSelectedEvents()){
				events += event + ",";
			}
			template.setEvents(events);
		}
		template.verify();
	}
	
	public WFTemplate getWFTemplate(WorkflowAware workflowAware) {
		WFTemplate model = new WFTemplate();
		List<WFTemplate> templates = genericService.searchAll(model)
				.getData();
		for (WFTemplate wftemplate : templates) {
			WFTemplate template = wftemplate.getTemplate(workflowAware);
			if (template != null) return template;
		}
		return null;
	}
	
	
	public List<Map<String,String>> getSrcEventItems(){
		return srcEventItems;
	}
	
	public void register(Class<WorkflowAware> event , String title){
		if (modelService.getModel(event) == null) return ;
		Map<String ,String> types =  new HashMap<String , String>();
		String simpleName = event.getSimpleName();
		if (simpleName.equals("Delivery")){
			simpleName = "Instock";
		}
		types.put("id", simpleName);
		types.put("name", title);
		srcEventItems.add(types);
		
		//TODO: perhaps init privilege in builder env.
		//add privilege
		String modelSimpleName = modelService.getModelName(event);
//		if (modelSimpleName.equals("espirit.logistics.receiving")){
//			modelSimpleName = "espirit.logistics.instock";
//		}
		I18n gonext = i18nService.addI18n("提交", "Gonext");
//		I18n approve = i18nService.addI18n("审批", "Approve");
		privilegeService.addMethodScopePrivile(modelSimpleName, FunctionButton.GONEXT, ScopeType.Global, gonext.getKey());
//		privilegeService.addMethodScopePrivile(modelSimpleName, FunctionButton.APPROVE, ScopeType.Global, approve.getKey());

	}
}
